package com.paomi.client.activity.receive.adapter;

import android.app.Activity;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.activity.person.model.CardJsonBean;
import com.paomi.client.utils.Util;
import com.paomi.client.widget.RxToast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CardReceiveListAdapter extends RecyclerView.Adapter<CardReceiveListAdapter.DefaultViewHolder> {

    private Activity context;
    private Typeface mTypeface;
    private List<CardJsonBean> data = new ArrayList<>();
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    public CardReceiveListAdapter(Activity context, Typeface typeface) {
        this.context = context;
        this.mTypeface = typeface;
    }

    public void setData(List<CardJsonBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @NonNull
    @Override
    public DefaultViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_receive_card, parent, false);
        return new DefaultViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DefaultViewHolder holder, int position) {
        CardJsonBean bean = data.get(position);
//        int width = Util.getDisplay(context).widthPixels - Util.dip2px(context, 30);
//        Util.setWidthAndHeight(holder.rl_ls, (int) (width * 0.27), -1);
//        Util.setWidthAndHeight(holder.rl_lx, (int) (width * 0.27), -1);
//        Util.setWidthAndHeight(holder.rl_rs, (int) (width * 0.73), -1);
//        Util.setWidthAndHeight(holder.rl_rx, (int) (width * 0.73), -1);


//        if (bean.getType() == 1) {
//            GlideApp.with(context).asBitmap().load(R.mipmap.img_card_you_y).into(holder.iv_img);
//        } else {
//            GlideApp.with(context).asBitmap().load(R.mipmap.img_card_piao_y).into(holder.iv_img);
//        }
        holder.shop_name.setText("满" + bean.getEnableAmount() + "使用");
        if( bean.getAssName()!=null){
            holder.num_pays.setText(context.getResources().getString(R.string.card_assName) + bean.getAssName());
        }
        holder.shop_date.setText(context.getResources().getString(R.string.card_data)
                + new SimpleDateFormat("yyyy-MM-dd").format(bean.getExpireDdate() * 1000));
        if (bean.getSelect() == 0) {
            holder.shop_btn.setText(context.getResources().getString(R.string.my_vip_btn));
            holder.shop_btn.setBackgroundResource(R.drawable.bg_receive_right);
        } else if (bean.getSelect() == 1) {
            holder.shop_btn.setText(context.getResources().getString(R.string.my_vip_un_btn));
            holder.shop_btn.getBackground().setAlpha(100);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < data.size(); i++) {
                    if (i == position) {
                        if(data.get(i).getSelect()==1){
                            data.get(i).setSelect(0);
                        }else {
                            data.get(i).setSelect(1);
                        }
                    } else {
                        data.get(i).setSelect(0);
                    }
                    notifyDataSetChanged();
                }

            }
        });


    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class DefaultViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.shop_name)
        TextView shop_name;
        @BindView(R.id.num_pays)
        TextView num_pays;
        @BindView(R.id.shop_date)
        TextView shop_date;
        @BindView(R.id.shop_btn)
        TextView shop_btn;


        public DefaultViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
//        void setOnItemClick(int pos);
    }
}
